package com.cssy.yd.logic.web;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.yd.basic.service.IBasicStorageService;
import com.cssy.yd.logic.entity.LogicWhAffstockDO;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/yd/logic/warehouseStock")
public class WarehouseStockController extends BaseController<LogicWhAffstockDO>{
	
	private static final String MANAGER = "/yd/logic/warehouseStock/Manager";
	private static final String MANAGERCONTAINMONEY = "/yd/logic/warehouseStock/ManagerContainMoney";
	
	@Resource(name="logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Resource(name = "basicwhService")
	private IBasicStorageService basicwhService;
	
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		String containMoney = request.getParameter("containMoney");
		List<String> lists = basicwhService.getStorageinfoAndArea();
		model.addAttribute("lists", lists);
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		if("true".equals(containMoney)){
			return MANAGERCONTAINMONEY;
		}
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		 SearchConditions scs = new SearchConditions();
		 hander.getWheres(scs);	
		 Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"whCode");
		 scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		 SearchResult result= logicWhAffStockService.findForWareHouseStockByExample(scs);
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);
	}

	@Override
	public String operationForm(HttpServletRequest request, String id,
			String flag, Model model) throws Exception {
		return null;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicWhAffstockDO curAP, BindingResult result, Model model)
			throws Exception {
		return null;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		return null;
	}
	
	/**
	 * 获取物料在指定仓库中的总库存数
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getSumWhQuantity")
	@ResponseBody
	public double getSumQuantityByWhInfoAndMaInfo(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String maCode = request.getParameter("code");
		String whCode = request.getParameter("whCode");
		
		return logicWhAffStockService.getSumQuantityByWhInfoAndMaInfo(maCode, whCode);
	}
	
	/**
	 * 导出excel
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/exportExcel")
	public void exportExcel(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		scs.clearCondition();

		 Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"whCode");
		 scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		hander.setFilters(new String(request.getParameter("filters").getBytes("ISO-8859-1"), "UTF-8")); //转码
		hander.getWheres(scs);
		scs.setLimitPage(false);
		scs.setLimitClause(null);
		//String isShowMoney = request.getParameter("isShowMoney"); //是否显示金额
		
		boolean flag = false;
//		if("YES".equals(isShowMoney)){
//			flag = true;
//		}		
		logicWhAffStockService.exportExcel(scs,response,flag);
	}

}
